package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class DishContorller {

    @Autowired
    private DishService dishService;

    /**
     * 菜品列表分页查询
     */
    @GetMapping("/dish/page")
    private ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name
    ) {
        //1.调用service分页查询
        Page<Dish> page = dishService.findByPage(name, pageNum, pageSize);

        //返回结果
        return ResultInfo.success(page);
    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        dishService.save(dish);
        return ResultInfo.success(null);
    }

    //回显信息
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    //修改菜品信息
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        dishService.update(dish);
        return ResultInfo.success(null);
    }

    //批量起售/停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo deleteByIds(@PathVariable("status") Integer status, Long[] ids) {
        if (ids != null && ids.length > 0) {
            dishService.updateByIds(ids, status);
        }
        return ResultInfo.success(null);
    }

    //删除 批量删除
    @DeleteMapping("/dish")
    public ResultInfo deleteById(Long[] ids) {
        if (ids != null && ids.length > 0) {
            dishService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }

    //根据分类id和status=1查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findBycategory(Long categoryId, @RequestParam(defaultValue = "1") Integer status, String name) {
        List<Dish> dishList = dishService.findList(categoryId, name, status);
        return ResultInfo.success(dishList);

    }

}
